#! /usr/bin/python3.10
# coding=utf-8
# @Time：2025/9/9 15:55
# @Author：Shawn
# @introduction：
import csv
import time

import requests

# 数据（列表格式）
from MqttSign import AuthIfo
from get_device_triples_info import get_device_triples_info

data = [
    ["scada_sn", "ems_sn", "username", "password", "client_id"],
]
res_login_get_tokon = requests.post(url="https://huiduems.alphaess.com/api/usercenter/cloud/user/login",
                                    json={"username": "admin",
                                          "password": "Y89JW9K0u/3Q1D2UxcWiTPOA6mDororCSGVzeCS9qHQ="},

                                    )
token = res_login_get_tokon.json()["data"]["token"]
print(token)

# 删除数据使用
for i in range(1, 5001):
    device_name = "AF5000202600" + str(i).zfill(4)

    res_delete = requests.delete(url="https://sgcni.alphaess.com/api/ci-web/snManage/deleteSn",
                                 params={"sysSn": device_name}, headers={"authorization": token})
    print(res_delete.json())

"""

for i in range(1, 5001):
    print()
    device_name = "AF5000202600" + str(i).zfill(4)
    print(device_name)
    triples_info = get_device_triples_info(device_name, url="https://huidu-eurems.alphaess.com")
    # print(triples_info)

    parts = triples_info.split(';')
    # print(parts)
    # 提取分割后的三部分数据+scada_sn
    host = parts[0]  # 第一部分：主机地址
    product_key = parts[1]  # 第二部分：客户端ID
    device_secret = parts[2]  # 第三部分：密码/密钥

    client_id = f"{product_key}.{device_name}"

    time_stamp = str(int(time.time() * 1000))

    # 生成认证信息
    auth = AuthIfo()
    auth.calculate_sign_time(product_key, device_name, device_secret, client_id, time_stamp)

    username = auth.mqttUsername
    password = auth.mqttPassword
    mqtt_client_id = auth.mqttClientId

    print(device_name, device_name + "01", username, password, mqtt_client_id)

    # 添加到CSV的列表data中
    data.append([device_name, device_name + "01", username, password, mqtt_client_id])

# 写入CSV文件
with open("connection_iot_info.csv", "w", newline='', encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerows(data)

"""